package com.kordar.admin.modular.sys.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kordar.admin.enums.CommonActive;
import com.kordar.admin.enums.EnumUtils;
import com.kordar.admin.kernel.wrapper.BatchDeleteWrapperEntity;
import com.kordar.admin.kernel.wrapper.UpdateWrapperEntity;
import com.kordar.admin.modular.sys.entity.SettingEntity;
import com.kordar.admin.modular.sys.enums.SettingPosition;
import com.kordar.admin.modular.sys.enums.SettingType;
import com.kordar.admin.modular.sys.service.SettingService;
import com.kordar.datalist.DatalistService;
import com.kordar.datalist.ResultEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("sys")
public class SettingController {

    final
    SettingService settingService;

    final
    DatalistService datalistService;

    public SettingController(SettingService settingService, DatalistService datalistService) {
        this.settingService = settingService;
        this.datalistService = datalistService;
    }

    @ModelAttribute("pidLabels")
    public String getMenuTree() {
        return JSON.toJSONString(settingService.pidLabels());
    }

    @ModelAttribute("activeLabels")
    public Map<Integer, String> active() {
        return EnumUtils.EnumToMapInt(CommonActive.class);
    }

    @ModelAttribute("typeLabels")
    public Map<Integer, String> type() {
        return EnumUtils.EnumToMapInt(SettingType.class);
    }

    @ModelAttribute("positionLabels")
    public Map<Integer, String> position() {
        return EnumUtils.EnumToMapInt(SettingPosition.class);
    }

    @GetMapping("setting/index")
    public String index(Model model) {
        model.addAttribute("data", settingService.list());
        return "pages/modular/system/setting/index";
    }

    @GetMapping("setting/form")
    public String form(Model model, @RequestParam(required = false, defaultValue = "0") Integer id) {
        if (id == 0) {
            model.addAttribute("header", "新建项目");
            model.addAttribute("setting", new SettingEntity());
        } else {
            model.addAttribute("header", "修改项目");
            model.addAttribute("setting", settingService.getById(id));
        }

        return "pages/modular/system/setting/form";
    }

    @PostMapping("setting/save")
    @ResponseBody
    public ResultEntity<Object> save(@Validated SettingEntity settingEntity) {
        if (settingEntity.getId() == 0) {
            settingEntity.setId(null);
            settingService.save(settingEntity);
        } else {
            settingService.updateById(settingEntity);
        }
        return new ResultEntity<>();
    }

    @RequestMapping("setting/edit")
    @ResponseBody
    public ResultEntity<Object> edit(@RequestBody UpdateWrapperEntity<SettingEntity> entity) {
        UpdateWrapper<SettingEntity> query = entity.where(SettingEntity.class).data().getQuery();
        settingService.update(query);
        return new ResultEntity<>();
    }

    @PostMapping("setting/delete")
    @ResponseBody
    public ResultEntity<Object> del(long id) {
        settingService.removeById(id);
        return new ResultEntity<>();
    }

    @RequestMapping("setting/batch-delete")
    @ResponseBody
    public ResultEntity<Object> del(@RequestBody BatchDeleteWrapperEntity<Integer> entity) {
        Set<Integer> ids = entity.getIds();
        settingService.removeByIds(ids);
        return new ResultEntity<>();
    }

}
